export function mapGetters(gettersArr) {
  let obj = {};
  for (const gettersName of gettersArr) {
    obj[gettersName] = function() {
      return this.$store.getters[gettersName];
    };
  }
  return obj;
}
export function mapState(statesArr) {
  let obj = {};
  for (const statesName of statesArr) {
    obj[statesName] = function() {
      return this.$store.state[statesName];
    };
  }
  return obj;
}

export function mapMutations(mapMutationsArr) {
  let obj = {};
  for (const ationsName of mapMutationsArr) {
    obj[ationsName] = function(data) {
      this.$store.commit(ationsName, data);
    };
  }
  return obj;
}

//  subscribe 定阅
//replaceState 替换状态
export const plugin = () => {
  return (store) => {
    //先判断一下
    let loc = JSON.parse(localStorage.getItem("@vuex"));
    if (loc) {
      store.replaceState(loc);
    }
    //存放数据
    store.subscribe((type, state) => {
      //   console.log(type,state)
      localStorage.setItem("@vuex", JSON.stringify(state));
    });
  };
};
